package com.demo.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 *  Spring 框架中的工具类，用于在非 Spring 管理的类中获取 Spring 容器中的 Bean 实例。
 *   它通过实现ApplicationContextAware接口来获取 Spring 的应用上下文（ApplicationContext）
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHolder.applicationContext = applicationContext;
    }
    public static <T> T getBean(Class<T> clazz){
        return ApplicationContextHolder.applicationContext.getBean(clazz);
    }
}
